/**
 * Created by pactera on 16/12/28.
 */
module.exports = function ($rootScope,$scope,$stateParams,$state,$cookies,confirmTeamService,registerService,httpservice,$interval){

    $scope.disabled = false;
    $scope.passwordShow = true;
    $scope.teamTypeName = "";
    $scope.userInfo={};
    $scope.verifyCode='';
    $scope.sendMessage = {
    disable: true,
    text: '发送验证码'
    };
  //获取邮箱验证码
  $scope.emailUrl = httpservice.getEmailRegisterCodeUrl +"?time="+(new Date());
  //点击图片,更新验证码
  $scope.getEmailRegisterCode = function () {
    $scope.emailUrl =  httpservice.getEmailRegisterCodeUrl +"?time="+(new Date());
  };

    $scope.init = function () {

        $scope.password = "";
        $scope.email = "";
        $scope.teamName = "";
      var paras = getRequestParas();
        $scope.phoneNum =paras.phoneNum;
        $scope.token = paras.token;
        $scope.type = paras.type;
        // alert("phoneNum:" + $scope.phoneNum + ",emailNum:" + $scope.emailNum + ",token:" + $scope.token + ",type:" + $scope.type);

        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        confirmTeamService.getInviteInfo($scope.password, $scope.phoneNum, $scope.emailNum, $scope.token, $scope.type).then(function(data){

            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if (data.code == 1){
                $scope.email = data.data.email;
                $scope.teamName = data.data.teamName;
                $scope.passwordShow = data.data.resetFlag;
                if ($scope.type == 'teamInvite' )
                  $scope.teamTypeName = '团队';
                else if ($scope.type == 'lawFirmInvite' )
                  $scope.teamTypeName = '律所';
                else if ($scope.type == 'caseInvite' )
                  $scope.teamTypeName = '案件';
                $scope.disabled = false;
            }else {
                $('.error_pwd').fadeIn();

                $('.error_pwd').children('p').html(data.msg);
                $scope.disabled = true;
            }
        },function (response) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            console.error('律所或者团队取得信息失败', response.status, response.statusText);
        });
        // $scope.emailNum = $stateParams.emailNum;
        // $scope.token = $stateParams.token;
    };
  function getRequestParas() {
    var res = new Object();
    var url = window.location.href; //获取url中"?"符后的字串
    if (url.indexOf("?") != -1) {
      url = url.substring(url.indexOf("?") + 1,url.length);
      var str = url.split('&');
      for(var index = 0; index < str.length + 1; index++) {
        var cc = str[index];
        if(index == 0) {

          res.token = cc.split('=')[1];
        }
        if(index == 1) {

          res.type = cc.split('=')[1];
        }
        /*if(index == 2){
          res.phoneNum = cc.split('=')[1];
        }*/
      }
    }
    return res;
  }

    $scope.init();

    $scope.reset = function(){
        $('.password').removeClass('writeError');
        $('.error_pwd').fadeOut();
        // if ($scope.password != ''){
      var phOneNum='';
       if($scope.type=='forgetPassword'){
         phOneNum=$scope.phoneNum;
       }else{
         phOneNum=$scope.userInfo.cellphone;
       }
            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            confirmTeamService.resetpassword($scope.password, phOneNum, $scope.emailNum, $scope.token, $scope.type,$scope.verifyCode,$scope.userInfo.msgCode).then(function(data){

                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if (data.code == 1){
                    $state.go('successTeam', {teamName:$scope.teamName});
                }else {
                    $('.error_pwd').fadeIn();
                    $('.error_pwd').children('p').html(data.msg);
                }
            },function (response) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                console.error('律所或者团队激活失败', response.status, response.statusText);
            });
        // } else {
        //     $('.password').addClass('writeError');
        //     $('.error_pwd').fadeIn();
        // }
    };

  /*发送验证发*/
  $scope.sendCode=function() {
      $scope.sendMessage.disable = true;
      //发送
      registerService.sendCode($scope.userInfo.cellphone,$scope.verifyCode).then(function (response) {
        if (response.data.code == 1) {
          //发送成功，开始倒计时
          var times = 60;
          $scope.sendBtnDisable = true;
          var coutdown = $interval(function () {
            if (times > 1) {
              times--;
              $scope.sendMessage.disable = true;
              $scope.sendMessage.text = " 重新发送 (" + times + "S) ";
            } else {
              $interval.cancel(coutdown);
              $scope.sendMessage.disable = false;
              $scope.sendMessage.text = " 重新发送 ";
            }
          }, 1000);
        } else {
          $scope.sendMessage.disable = true;
          $('#vcode').addClass('writeError');
          $('.error_emailNum').fadeIn();
          $('.error_emailNum').children('p').html(response.data.msg);
        }
      })
  };
  $scope.checkPhoneNum=function(){
    var cellPhone=/^1(3|4|5|7|8)[0-9]\d{8}$/;
    if($scope.userInfo.cellphone!=''&&$scope.userInfo.cellphone!=undefined){
      $('.error_emailNum').fadeOut();
      $('.error_emailNum').children('p').html("");
      if($scope.userInfo.cellphone.match(cellPhone)){
        $scope.sendMessage.disable = false;
        $('.error_emailNum').fadeOut();
        $('.error_emailNum').children('p').html("");
      }else{
        $scope.sendMessage.disable = true;
        $('.error_emailNum ').fadeIn();
        $('.error_emailNum').children('p').html("请输入正确的手机号");
      }
    }else{
      $scope.sendMessage.disable = true;
      $('input[name="phonenum"]').addClass('writeError');
      $('.error_emailNum ').fadeIn();
      $('.error_emailNum').children('p').html("手机号不能为空");
    }
  };


  /**
   * 查询验证码长度
   */
  $scope.sendMessage.disableT = true;
  $scope.checkVerifyCode = function () {
    if ($scope.verifyCode.length == 4) {
      $('.error_emailNum').fadeOut();
      $('.error_emailNum').children('p').html("");
      $scope.sendMessage.disableT = false;
      if($scope.userInfo.cellphone!=''&&$scope.userInfo.cellphone!=undefined){
        $scope.sendMessage.disable = false;
      }else{
        $scope.sendMessage.disable = true;
        $('.error_emailNum ').fadeIn();
        $('.error_emailNum').children('p').html("手机号不能为空");
      }

    }else {
      $('.error_emailNum').fadeIn();
      $('.error_emailNum').children('p').html("验证码应为4位");
      $scope.sendMessage.disableT = true;
    }
  };




};
